﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace GestionePolveri2.Reports
{
    public partial class ReportViewerDialog2 : Form
    {
        public event EventHandler<ReportErrorEventArgs> ReportError;

        private string m_report = string.Empty;
        private object m_datasource = null;

        public ReportViewer ReportViewer
        {
            get { return this.reportViewer1; }
        }

        public string Report
        {
            get { return m_report; }
            set { m_report = value; }
        }

        public object Datasource
        {
            get { return m_datasource; }
            set { m_datasource = value; }
        }

        public ReportViewerDialog2()
        {
            InitializeComponent();

            this.btnClose.Click += new EventHandler(btnClose_Click);
        }

        private void ReportViewerDialog2_Load(object sender, EventArgs e)
        {
            //this.reportViewer1.RefreshReport();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void ShowReport()
        {
            if (m_report == string.Empty)
            {
                MessageBox.Show("Indicare il nome del report !", "Report viewer Dialog", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (m_datasource != null)
            {
                //reportViewer1.ReportError += new ReportErrorEventHandler(reportViewer1_ReportError);
                //Microsoft.Reporting.WinForms.ReportViewer reportViewer = this.reportViewer1;

                reportViewer1.LocalReport.DataSources.Clear();

                // Set Processing Mode
                reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;

                // Set RDL file
                reportViewer1.LocalReport.ReportPath = string.Format(@"{0}\Reports\{1}.rdlc", Application.StartupPath, m_report);

                // Supply data corresponding to each report data source.
                reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("ReportData", m_datasource));

                // Process and render the report
                reportViewer1.RefreshReport();
            }
            else
            {
                reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("ReportData", m_datasource));
                this.reportViewer1.LocalReport.DataSources.Clear();
                this.reportViewer1.RefreshReport();

                MessageBox.Show("Nessun dato trovato!");
            }

            this.Show();
        }

        //public void reportViewer1_ReportError(object sender, ReportErrorEventArgs e)
        //{
        //    if (ReportError != null)
        //    {
        //        ReportError(sender, e);
        //    }
        //}
    }
}
